package dl.jf.service.impl.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MUser;
import dl.jf.dao.idao.ICommonDao;
import dl.jf.dao.idao.admin.IAdminMemberDao;
import dl.jf.service.iservice.ICommonService;
import dl.jf.service.iservice.admin.IAdminMemberService;
import dl.jf.service.util.Toolkit;

public class AdminMemberServiceImpl implements IAdminMemberService
{
	private IAdminMemberDao adminMemberDao;
	
	public void setAdminMemberDao(IAdminMemberDao adminMemberDao) {
		this.adminMemberDao = adminMemberDao;
	}

	@Override
	public ArrayList<LabelValue> getRoleLabelListById() throws Exception {
		return getRoleLabelListById(null);
	}
	
	@Override
	public ArrayList<LabelValue> getRoleLabelListById(Integer roleId) throws Exception {
		return adminMemberDao.getRoleLabelListById(roleId);
	}
	
	@Override
	public Map<String, Object> getUserListByUser() throws Exception {
		return getUserListByUser(new MUser());
	}
	
	@Override
	public MUser getUserListByUserId(String userId) throws Exception {
		return adminMemberDao.getUserListByUserId(userId);
	}
	
	@Override
	public Map<String, Object> getUserListByUser(MUser user) throws Exception {
		if(!Toolkit.isEmpty(user.getUserId())){
			user.setUserId("%" + user.getUserId() + "%");
		}
		if(!Toolkit.isEmpty(user.getUserName())){
			user.setUserName("%" + user.getUserName() + "%");
		}
		if(!Toolkit.isEmpty(user.getEmailAddress())){
			user.setEmailAddress("%" + user.getEmailAddress() + "%");
		}
		
		int count = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		if(user.getEndNo() != null) {
			count = adminMemberDao.getUserListByUserTotal(user);
			map.put("count", count);
		}
		map.put("list", adminMemberDao.getUserListByUser(user));
		
		return map;
	}
	
	@Override
	public String insertUser(MUser user) throws Exception {
		String userId = adminMemberDao.insertUser(user);
		return userId;
	}

	@Override
	public int updateUserByUserId(MUser user) throws Exception {
		int count = adminMemberDao.updateUserByUserId(user);
		return count;
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public int deleteUserByUserIdArray(String[] array) throws Exception{
		int i = 0;
		for(; i < array.length; i++){
			adminMemberDao.deleteUserByUserId(array[i]);
		}
		return i;
	}
}
